package com;

import com.alibaba.fastjson.JSONObject;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 建议在idea写好复制整个类到此处，位置report-core/src/test/java/com/DemoGroovyHandler.java
 */
public class DemoGroovyHandler implements IGroovyHandler {

    private static final String JINLING_CODE = "1210";

    @Override
    public List<JSONObject> transform(List<JSONObject> data) {
        // 全基地 时要去掉 吉林山鹰
        boolean allFactoryFlag = false;
        if (data == null || data.size() == 0) {
            return data;
        } else {
            List<String> collect = data.stream().map(o -> o.getString("factoryCode")).distinct().collect(Collectors.toList());
            if (collect.size() > 1) {
                data = data.stream().filter(o -> !JINLING_CODE.equals(o.getString("factoryCode"))).collect(Collectors.toList());
            }
        }
        Map<Integer, List<JSONObject>> dateMap = data.stream().collect(Collectors.groupingBy(o -> o.getInteger("date")));

        List<JSONObject> results = new ArrayList<>(dateMap.size());
        dateMap.forEach((k, list) -> {
            JSONObject temp = new JSONObject();
            temp.put("date", k);
            BigDecimal weight = list.stream().map(o -> o.getBigDecimal("weight")).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(0, BigDecimal.ROUND_FLOOR);
            temp.put("weight", weight);
            BigDecimal count = list.stream().map(o->o.getBigDecimal("count")).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal price = list.stream().map(o -> o.getBigDecimal("price")).reduce(BigDecimal.ZERO, BigDecimal::add);
            price = price.divide(count).setScale(0, BigDecimal.ROUND_FLOOR);
            temp.put("price", price);
            results.add(temp);
        });
        return results;
    }

    public static void main(String[] args) {

        String json = "[\n" +
                "  {\n" +
                "    \"date\": 1,\n" +
                "    \"factoryCode\": \"1210\",\n" +
                "    \"price\": 1665,\n" +
                "    \"weight\": 2365\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 1,\n" +
                "    \"factoryCode\": \"1110\",\n" +
                "    \"price\": 1630,\n" +
                "    \"weight\": 90888\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 1,\n" +
                "    \"factoryCode\": \"1150\",\n" +
                "    \"price\": 1675,\n" +
                "    \"weight\": 8187\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 1,\n" +
                "    \"factoryCode\": \"1190\",\n" +
                "    \"price\": 1629,\n" +
                "    \"weight\": 490\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 1,\n" +
                "    \"factoryCode\": \"1220\",\n" +
                "    \"price\": 1607,\n" +
                "    \"weight\": 26745\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 2,\n" +
                "    \"factoryCode\": \"1210\",\n" +
                "    \"price\": 1630,\n" +
                "    \"weight\": 828\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 2,\n" +
                "    \"factoryCode\": \"1110\",\n" +
                "    \"price\": 1630,\n" +
                "    \"weight\": 33532\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 2,\n" +
                "    \"factoryCode\": \"1150\",\n" +
                "    \"price\": 1613,\n" +
                "    \"weight\": 2187\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 2,\n" +
                "    \"factoryCode\": \"1190\",\n" +
                "    \"price\": 1580,\n" +
                "    \"weight\": 78\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 2,\n" +
                "    \"factoryCode\": \"1220\",\n" +
                "    \"price\": 1582,\n" +
                "    \"weight\": 13344\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 3,\n" +
                "    \"factoryCode\": \"1210\",\n" +
                "    \"price\": 1578,\n" +
                "    \"weight\": 1810\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 3,\n" +
                "    \"factoryCode\": \"1110\",\n" +
                "    \"price\": 1567,\n" +
                "    \"weight\": 89389\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 3,\n" +
                "    \"factoryCode\": \"1150\",\n" +
                "    \"price\": 1603,\n" +
                "    \"weight\": 7574\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 3,\n" +
                "    \"factoryCode\": \"1190\",\n" +
                "    \"price\": 1497,\n" +
                "    \"weight\": 10266\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 3,\n" +
                "    \"factoryCode\": \"1220\",\n" +
                "    \"price\": 1540,\n" +
                "    \"weight\": 49205\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 4,\n" +
                "    \"factoryCode\": \"1100\",\n" +
                "    \"price\": 1530,\n" +
                "    \"weight\": 956\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 4,\n" +
                "    \"factoryCode\": \"1210\",\n" +
                "    \"price\": 1535,\n" +
                "    \"weight\": 96805\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 4,\n" +
                "    \"factoryCode\": \"1150\",\n" +
                "    \"price\": 1622,\n" +
                "    \"weight\": 6600\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 4,\n" +
                "    \"factoryCode\": \"1190\",\n" +
                "    \"price\": 1560,\n" +
                "    \"weight\": 24719\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 4,\n" +
                "    \"factoryCode\": \"1220\",\n" +
                "    \"price\": 1539,\n" +
                "    \"weight\": 41720\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 5,\n" +
                "    \"factoryCode\": \"1210\",\n" +
                "    \"price\": 1530,\n" +
                "    \"weight\": 187\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 5,\n" +
                "    \"factoryCode\": \"1110\",\n" +
                "    \"price\": 1500,\n" +
                "    \"weight\": 43466\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 5,\n" +
                "    \"factoryCode\": \"1150\",\n" +
                "    \"price\": 1627,\n" +
                "    \"weight\": 3665\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 5,\n" +
                "    \"factoryCode\": \"1190\",\n" +
                "    \"price\": 1542,\n" +
                "    \"weight\": 8094\n" +
                "  },\n" +
                "  {\n" +
                "    \"date\": 5,\n" +
                "    \"factoryCode\": \"1220\",\n" +
                "    \"price\": 1524,\n" +
                "    \"weight\": 22100\n" +
                "  }\n" +
                "]";
        List<JSONObject> list = JSONObject.parseArray(json, JSONObject.class);
        // list  根据 date字段 分组统计 weight 总和 和 price的平均值

        DemoGroovyHandler demoGroovyHandler = new DemoGroovyHandler();
        List<JSONObject> transform = demoGroovyHandler.transform(list);
        System.out.println(transform);
    }

}
